export default {
   namespaced: true,
   state: {
     // 点击商品详情页收藏按钮
     isCollect: {
       type: 'icon-heart',
       status: JSON.parse(uni.getStorageSync('isCollect') || false)
     },
     // 点击商品详情页喜欢按钮
     isLike: {
       type: 'icon-chevron-down-circle',
       status: JSON.parse(uni.getStorageSync('isLike') || false)
     },
     // 存储收货地址
     address: JSON.parse(uni.getStorageSync('address') || '[]'),
     token: '',
     userInfo: {}
   },
   mutations: {
     // 更改商品详情页收藏状态
     setCollect(state,payload) {
       state.isCollect.status = !state.isCollect.status
       uni.setStorageSync('isCollect', JSON.stringify(state.isCollect.status))
     },
     // 更改商品详情页喜欢状态
     setLike(state,payload) {
       state.isLike.status = !state.isLike.status
       uni.setStorageSync('isLike', JSON.stringify(state.isLike.status))
     },
     // 更改收货地址
     updateAddress(state,payload) {
      
      const addressResult = state.address.find(item => item.id === payload.id)
      let arrIndex = state.address.indexOf(addressResult)
      
      console.log(arrIndex);
      
      if(addressResult) {
        addressResult.consigneeName = payload.consigneeName
        addressResult.phone = payload.phone
        addressResult.consigneeRegion = payload.consigneeRegion
        addressResult.detailedAddress = payload.detailedAddress
        addressResult.labelDefault = payload.labelDefault
        addressResult.labelName = payload.labelName
        if(payload.switchShow) {
          state.address.forEach((item, i) => {
            item.switchShow = false
          })
        }
        addressResult.switchShow = payload.switchShow
        state.address.splice(arrIndex, 1)
        state.address.unshift(addressResult)
        return
      }
      // 判断新添加的地址是否为默认地址，是的话将其他的地址的默认取消
      if(payload.switchShow) {
        state.address.forEach((item, i) => {
          item.switchShow = false
        })
      }
      payload.id = state.address.length + 1
      state.address.push(payload)
      // const defaultState = state.address.find(item => item.switchShow)
      // console.log(defaultState);
      
      let simpleArr = state.address.filter((item, i) => {
      	return item.switchShow != true;
      })
      
      console.log(simpleArr);
     },
     // 1. 定义将 address 持久化存储到本地 mutations 方法
     saveAddressToStorage(state) {
       uni.setStorageSync('address',JSON.stringify((state.address)))
     }
   },
   actions: {
     // 更改收货地址
     updateAddress(ctx, address) {
       ctx.commit('updateAddress', address)
       ctx.commit('saveAddressToStorage')
     }
   }
}